<template> 
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="logo-name"><img src="../assets/logo.png" alt=""><span>电商后台管理系统</span></div> 
      <el-button type="info" @click="exit">退出</el-button></el-header>
    <el-container>
      <!-- 侧边区域 -->
      <el-aside :width="isCollapse? '64px':'200px'">
        <!-- 折叠菜单区域 -->
        <p class="toggle-button" @click="toggleMenu">|||</p>
        <el-menu class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#40EFFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="isPathActive">
          <!-- :default-active="$route.path" -->
          <!-- 一级下拉菜单 --> 
          <!-- index要不一样，不然点击一个下拉，所有状态都一样，同时index绑定字符串类型 -->
          <el-submenu :index="submenu.id+''" v-for="submenu in menuList" :key="submenu.id">
            <!-- 下拉菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[submenu.id]"></i>
              <!-- 文字 -->
              <span>{{submenu.authName}}</span>
            </template>
            <!-- 二级菜单项 -->
            <el-menu-item @click="saveNaveState('/'+submenu2.path)" :index="'/'+submenu2.path" v-for="submenu2 in submenu.children " :key="submenu2.id">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文字 -->
                <span>{{submenu2.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主题区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import {homeMenu} from '../networks/home'
import Welcome from './homechild/Welcome'
export default {
  name: 'Home',
  components: {
    Welcome,
  },
  data() {
    return {
      //菜单数据
      menuList:[],
      //一级图标数据
      iconObj:{
        '125':'iconfont icon-yonghu1',
        '102':'iconfont icon-dingdan',
        '103':'iconfont icon-quanxian',
        '101':'iconfont icon-shangpin',
        '145':'iconfont icon-tongji'
      },
      //菜单是否折叠默认不折叠
      isCollapse:false,
      //活跃的路径
      isPathActive:'',
    }
  },
  created(){
    //组件创建时请求数据
    //请求菜单数据
    homeMenu().then(res=>{
      // 保存菜单数据
      if(res.meta.status !==200) return this.$message.error(res.meta.mes)
      this.menuList=res.data;
      // console.log("menuList",this.menuList);
    });
    //sessionStorage中的保存的index给ispathactive
    this.isPathActive=window.sessionStorage.getItem("activePath");
    
  },
  methods: {
    exit(){
      //销毁token
      window.sessionStorage.clear();
      //强制返回login
      this.$router.push("/login");
    },
    //切换菜单
    toggleMenu(){
      this.isCollapse=!this.isCollapse;
    },
    saveNaveState(activePath){
      window.sessionStorage.setItem('activePath',activePath);
      //保存当前活跃的路径
      this.isPathActive=window.sessionStorage.getItem("activePath");

    }
  },  
}
</script>
<style scoped>
.home-container{
  height: 100%;
}
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color:#fff;
  font-size: 20px;
  padding-left: 0;
}
.el-aside{
  background-color: #333744;
}
.el-menu{
  border-right: 0;
}
.el-main{
  background-color: #eaedf1;
}
img{
  width:40px;
  height: 100%;
  margin-right: 20px;
  border-radius: 50%;
}
.logo-name{
  display: flex;
  justify-content: center;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  width: 100%;
  text-align: center;
  color: #fff;
  font-size: 12px;
  /* line-height: 20px; */
  letter-spacing: 0.2em;
  /* 鼠标手指状态 */
  cursor: pointer;
}

</style>
